$(async () => {
  const campaign = 'invite-one'
  const av = 'p013'
  utils.initAnalytics(av)
  analytics.send({
    event: `${av}-open`,
    attr: {
      _c: utils.getUrlParam('_c')
    }
  })
  utils.initWechatShare()
  const account = new Account()
  utils.supportPromise(account)
  const userInfo = await account.$getUserInfo(true)
  const inviteInfo = await account.$ajax({
    type: 'GET',
    path: '/s/events/inviteOneOnOne'
  })
  console.log(inviteInfo)
  if (inviteInfo.status === 0) {
    $('.share-now').text('立即邀请')
    utils.initWechatShare({
      title: `好友${userInfo.nickname}送你3节久趣1对1北美外教课，价值450元！`,
      desc: '100%北美外教，让4-12岁孩子爱上英语！',
      link: `${window.location.protocol}//${window.location.host}/wx/invite-one-result${Math.ceil(Math.random() * 100)}?_c=${campaign}&_r=${userInfo.shareFlag}&rid=${inviteInfo.code}`,
      imgUrl: 'https://static-app.97kid.com/site-student/public/mobile/campaign/inviteOne/icon/ic_share.png',
      success: target => {
        analytics.send({
          event: `${av}-share`,
          attr: {target}
        })
      }
    })
  } else {
    $('.share-now').text(inviteInfo.status === 1 ? '已全部赠送' : '特权已过期')
    utils.initWechatShare({
      title: '【5折福利】久趣1对1北美精英外教在线教学，仅500名额！',
      desc: '适合4-12岁，专属外教固定时间，让孩子爱上英语！',
      link: `${window.location.protocol}//${window.location.host}/wx/2one${Math.ceil(Math.random() * 100)}?_c=${campaign}&_r=${userInfo.shareFlag}`,
      imgUrl: 'https://static-app.97kid.com/site-student/public/mobile/campaign/1v1/img/share.jpg',
      success: target => {
        analytics.send({
          event: `${av}-share`,
          attr: {target}
        })
      }
    })
  }
  renderTickets(inviteInfo.left)

  // 渲染tickets
  function renderTickets (num) {
    let html = ''
    for (let i = 0; i < 3; i++) {
      html +=`<img src="${RESOURCE}/img/ticket-${i < num ? 'ok' : 'no'}.png?v=${VERSION}" class="card">`
    }
    $('.card-list').empty().append(html)
  }

  // 立即分享
  $('.share-now').off('click').on('click', e => {
    $('.share-mask').show()
  })
  // 隐藏分享
  $('.share-mask').off('click').on('click', e => {
    $('.share-mask').hide()
  })
})